<script setup lang="ts">
import { getCurrentInstance, ref, onMounted } from 'vue'
const { proxy } = getCurrentInstance()

/* ---- */
const userInfo = proxy.$Store.get('userInfo') || {}
const logUser = proxy.$Store.get('logUser') || {}
const WXConfig = proxy.$Store.get('WXConfig') || {}

/* --设置微信分享功能-- */
proxy.$WxTool.loadJsdk(WXConfig, (obj) => {
  if (!obj) return
  // ----
  obj.hideMenuItem()
})

/* ---- */
const layer = ref()

/* ---- */
const setPublicInfo = async (item, callback) => {
  await proxy.$http[item.type](item.url, item.data).then((res) => {
    if (res) callback && callback(res)
  }).catch((err) => { proxy.$Myfun.forToast(layer.value, 'open', err) })
}
// ----
const getCurrencyInfo = async () => {
  // ----
  if (proxy.$route.query.forAttr && proxy.$route.query.forVal) {
    const forData = {
      config_id: userInfo.config_id,
      openid: userInfo.openid
    }
    const attrArr = proxy.$route.query.forAttr.split(',')
    const valArr = proxy.$route.query.forVal.split(',')
    attrArr.forEach((item, index) => {
      if (Number(index) > 0 && proxy.$route.query.forType) {
        const trueNum = Number(logUser[item]) || 0
        forData[item] = Number(trueNum + valArr[index])
      } else {
        forData[item] = Number(valArr[index])
      }
    })
    await setPublicInfo({
      type: 'patch',
      url: 'dealer/update?id=' + logUser.id,
      data: forData
    })
  }
  // ----
  if (proxy.$route.query.jumpName) {
    const forJumpExt = proxy.$route.query.jumpExt ? JSON.parse(proxy.$route.query.jumpExt) : null
    proxy.$Myfun.openPage(proxy, 'push', proxy.$route.query.jumpName, forJumpExt)
  } else {
    proxy.$router.back()
  }
}

/* ---- */
onMounted(() => {
  getCurrencyInfo()
})
</script>

<template>
  <layer-view ref="layer"/>
</template>
